package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.DynamicViewHolder;
import where2go.sctu.com.zhiyou.entity.Dynamic;
import where2go.sctu.com.zhiyou.entity.User;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.adpater
 * Created by 张某人
 * CreatedTime:on 2018/5/7   17:36
 * Description: 动态适配器
 */

public class DynamicAdapter extends RecyclerView.Adapter<DynamicViewHolder> {


    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


    private String url = "http://47.104.80.155/youyiyou/resources/img/";
    private Context context;
    private List<Dynamic.DynamicDataBean> dataList = new ArrayList<Dynamic.DynamicDataBean>();
    private List<User> userList = new ArrayList<User>();

    public DynamicAdapter(Context context, List<Dynamic.DynamicDataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public DynamicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.dynamic_item,
                parent, false);
        DynamicViewHolder holder = new DynamicViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final DynamicViewHolder holder, int position) {
        Dynamic.DynamicDataBean dataBean = dataList.get(position);
        holder.dynamicTitle.setText(dataBean.getDynamicTitle());
        holder.introduction.setText(dataBean.getDynamicContent());
        String s=format.format(new Date(Long.parseLong( dataBean.getDynamicDate())));
        holder.tTime.setText(s);
        holder.name.setText(dataBean.getUser().getUserName());
        ImageRequest imageRequest = new ImageRequest(url + dataList.get(position).getUser().getUserAvatarAddress(),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        holder.avatar.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片

                holder.avatar.setImageResource(R.mipmap.ic_launcher);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public void addList(List<Dynamic.DynamicDataBean> list) {
        dataList.addAll(list);
    }
}
